<script src="/doseyeh/web/js/jquery.autocomplete.js"></script> 

<div class="gray_box">
    <?php include_partial('tabs', array('selected' => $sf_request->getParameter('action'))) ?>
    <div class="registration_wrap">
        <br/>
        <div class="inner_blue_title"><?php echo __('My Doseyeh') ?></div>
        <br/>
        <div class="inner_text"><?php echo __('Please select the courses you are enrolled in this semester') ?></div>
        <br/>
        <?php include('flashes.php') ?>
        <br/>
        <br/>
        <?php if (!$disable_form) { ?>
            <form id="add_doseyeh_form"method="post" action="<?php echo url_for('user/my_doseyeh') ?>" onsubmit="return confirmSubmit()">
            <?php } ?>
            <div id="my_doseyeh_wrap" class="registration_form_wrap my_doseyeh_wrap">
                <div id="course_instructor_holder">
                    <?php if (count($current_courses) > 0) { ?>
                        <?php foreach ($current_courses as $k => $one) { ?>
                            <?php include_component('user', 'course_instructor', array('delete_button' => !$disable_form, 'index' => $k + 1, 'course_id' => $one['course_id'], 'instructor_id' => $one['instructor_id'], 'course_instructor' => $course_instructor)) ?>
                        <?php } ?>
                    <?php } else { ?>
                        <?php include_component('user', 'course_instructor', array('index' => 1, 'delete_button' => !$disable_form)) ?>
                    <?php } ?>
                </div>
                <br/>
                <?php if (!$disable_form) { ?>
                    <a href="#add_more" onclick="return add_more()" class="add_more"><?php echo __('Add More') ?></a>
                    <div class="aright"><button class=dbutton><?php echo __('Save') ?></button></div>			
                <?php } ?>
            </div>
            <?php if (!$disable_form) { ?>
            </form>
        <?php } ?>
    </div>
    <div id="dialog" title="Doseyeh" style="display: none">
        <p><?php echo __('Click OK to confirm your courses, you will not be able to add or delete any course during this semister.') ?></p>
        <div class="btn_div">
            <button onclick="document.getElementById('add_doseyeh_form').submit()">Ok</button>
            <button class="dialog-close">Cancel</button>
        </div>
    </div>
</div>
<div id="empty_row" style="display:none">
    <?php include_component('user', 'course_instructor', array('index' => $k + 1)) ?>
</div>
<script>
    html_before = '<div class="course_instructor_pair">';
    html_after = '</div>';
    all_html = html_before+$('#empty_row .course_instructor_pair').html()+html_after;
    
    current_index = <?php echo isset($k) ? $k : 1 ?>;
    
    function add_more(){
        current_index ++;
        all_html = all_html.replace('id="instructor_'+(current_index-1)+'"','id="instructor_'+current_index+'"');
        all_html = all_html.replace('id="autocomplete_user_dos_course_id','id="autocomplete_user_dos_course_id_'+current_index+'"');
        all_html = all_html.replace('id="user_dos_course_id','id="user_dos_course_id_'+current_index+'"');
        //        all_html = '<div class="new_row" style="display:none">'+all_html+'</div>';
        jQuery('#course_instructor_holder').append(all_html);
        $('select#instructor_'+current_index).selectmenu({style:'dropdown'});
        $('select#course_'+current_index).selectmenu({style:'dropdown'});
        //        $('.new_row').slideDown('300');
        return false;
    }

    function delete_ci_row(elem){
        $(elem).parents('.course_instructor_pair:first').remove();
        return false;
    }


    jQuery(document).ready(function() {
        
        $('#course_instructor_holder input[type=text]').live('keypress', function(e) {
            
            if($(this).val()==''){
        
                $(this).autocomplete('<?php echo url_for2("auto_complete_my_dosyeh") ?>', jQuery.extend({}, {
                    dataType: 'json',
                    parse:    function(data) {
                        var parsed = [];
                        for (key in data) {
                            parsed[parsed.length] = { data: [ data[key], key ], value: data[key], result: data[key] };
                        }
                        return parsed;
                    }
                }))
                .result(function(event, data) {
                    var html='<input type="hidden" id="user_dos_course_id" name="user_dos[]" value="'+data[1]+'">';
                    $("#"+this.id+"").next().html(html); 
            
        
                });
            }
          
        });
     
       
    });
     
     
    function confirmSubmit() {
        if ( $(function() {
            $( "#dialog" ).dialog();
        })) {
            //   document.getElementById("FORM_ID").submit();
        }
        return false;
    }
    
    $('.dialog-close').click(function(){
    
    $("html body div.ui-dialog").hide();
    
    });
    
    $('.dbutton').click(function(){
    
    $("html body div.ui-dialog").show();
    
    });
    

</script>
<?php if ($disable_form) { ?>
    <style>
        .ui-selectmenu-menu{display:none!important}
        .ui-selectmenu{cursor:default!important}
    </style>
<?php } ?>


